package top.linruchang.util;

import cn.hutool.core.util.ObjectUtil;
import top.linruchang.bean.FileAndRules;
import top.linruchang.bean.ReplaceStrAndRegexStr;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 作用：
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2020/11/03
 * @since 1.8
 */
public class RegexFileUtil {

    /**
     * <p>
     * description: 处理源文件
     * </p>
     *
     * @param sourceFile 源文件
     * @param fileAndRules 参考{@link FileAndRules} - 规则
     * @param isRecursive 如果sourceFile是目录 - 是否递归检索该目录下的所有文件 - 默认true
     * @return boolean
     */
    public static void regexReplaceFile(File sourceFile, List<FileAndRules> fileAndRules, boolean isRecursive, boolean isTranslate) {
        if(isRecursive) {



            List<File> subFiles = FileUtil.getSubFiles(sourceFile);
            subFiles.forEach(System.out::println);

            subFiles.forEach(sFile -> {
                //添加翻译正则
                fileAndRules.stream().filter(fileAndRule -> {
                    fileAndRule.addReplaceStrAndRegexStr(ReplaceStrAndRegexStr.chineseToEnglishContentRule());
                    return true;
                }).forEach(fileAndRule -> {
                    try {
                        FileUtil.fillFile(sFile, fileAndRule);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            });
        }else {
            fileAndRules.forEach(fileAndRule -> {
                try {
                    FileUtil.fillFile(sourceFile, fileAndRule);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
    public static void regexReplaceFile(String sourceFile, List<FileAndRules> fileAndRules, boolean isRecursive, boolean isTranslate) {
        regexReplaceFile(new File(sourceFile), fileAndRules, isRecursive, isTranslate);
    }
    public static void regexReplaceFile(String sourceFile, boolean isRecursive, boolean isTranslate, FileAndRules... fileAndRules) {
        if(ObjectUtil.isNotNull(fileAndRules)) {
            regexReplaceFile(sourceFile, Arrays.asList(fileAndRules), isRecursive, isTranslate);
        }else {

        }
    }

    /**迭代读取文件*/
    public static void regexReplaceFile(File sourceFile, List<FileAndRules> fileAndRules) {
        regexReplaceFile(sourceFile, fileAndRules, true,false);
    }
    public static void regexReplaceFile(String sourceFile, List<FileAndRules> fileAndRules) {
        regexReplaceFile(sourceFile, fileAndRules, true, false);
    }

}
